package date.Exa1;

import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TimeLine {
    public static void main(String[] args) {
        //时间戳
        Instant start = Instant.now();
        System.out.println(start.toEpochMilli());
        Instant test = Instant.ofEpochMilli(start.toEpochMilli());
        System.out.println(test);
        runAlgorithm();
        Instant end = Instant.now();
        //时间差
        Duration timeElapsed = Duration.between(start,end);
        long mills = timeElapsed.toMillis();
        System.out.println(mills+" milliseconds");
        Instant start2 = Instant.now();
        runAlgorithm2();
        Instant end2 = Instant.now();
        //end2-start2
        Duration timeElapsed2 = Duration.between(start2,end2);
        System.out.println(timeElapsed2.toMillis()+" millisecond");
        boolean overTenTimesFaster = timeElapsed2.isNegative();
        System.out.println("第二个时间差是否为负值？ "+overTenTimesFaster);
    }

    public static void runAlgorithm(){
        int size = 10;
        List<Integer> list = new Random().ints().map(i->i%100).limit(size).boxed().collect(Collectors.toList());
        Collections.sort(list);
        System.out.println(list);
    }

    public static void runAlgorithm2(){
        int size = 10;
        List<Integer> list = new Random().ints().map(i->i%100).limit(size).boxed().collect(Collectors.toList());
        while(!IntStream.range(1,list.size()).allMatch(i->list.get(i-1).compareTo(list.get(i))<=0)){
            Collections.shuffle(list);
        }

        System.out.println(list);
    }


}
